var page = 1;     //分页
var sort = "";    //查询类型
$(function () {
        // getCountTag();      //获取热门标签
        // getComment();       //获取有关自己的评论
})
/**
 * 随机查询页面
 */
function moreRandomArticle(){
    init();
    var randomArticle = $.ajax("selectAllArticle","get");
    randomArticle.done(function (result) {
        var articleList = result.extend.articleList;
        var cloneArticleSearch = cloneArticle(articleList,"更换信息");
        $("#articleSearch").append(cloneArticleSearch);
        $("#more").attr('onclick',"moreRandomArticle()");
        dianZan();
        window.scrollTo(0,0);   //点击后返回顶部
    })
}
/**
 * 初始化
 */
function init() {
    $("#articleSearch").empty();
    page = 1;
}

/**
 *  最新文章排序
 */
$("#dateSort").click(function () {
    init();
    sort = "article_date";
    $("#sort").html("最新");
    articleSort(sort);
})
/**
 * 点赞排序
 */
$("#zanSort").click(function () {
    init();
    sort = "zan_num";
    $("#sort").html("点赞");
    articleSort(sort);
})
/**
 * 收藏排序
 */
$("#collectSort").click(function () {
    init();
    sort = "collect_num";
    $("#sort").html("收藏");
    articleSort(sort);
})


/**
 * 排序查询
 * @param sort
 */
function articleSort() {
    var searchBySort = $.ajax({
        url: "searchBySort?sort=" + sort +"&page=" + page,
        type: "GET",
    })
    searchBySort.done(function (result) {
        var articleList = result.extend.articleList;
        var cloneArticleSearch = cloneArticle(articleList,"加载更多信息");
        page++;
        $("#articleSearch").append(cloneArticleSearch);
        dianZan();
    })
}

/**
 * 判断是否还有文章。
 */
function moreArticle(){
    var isMore = $.ajax({
        url: "searchBySort?sort=" + sort + "&page=" + page,
        type: "GET",
    })
    isMore.done(function (result) {
        if (result.code == 200){
            $("#more").html("没有更多的信息");
        } else {
            $("#more").remove();
            articleSort();
        }
    })
}

/**
 * 搜索按钮
 */
function search(){
    init();
    searchArticle();
}

/**
 * 搜索文章
 */
function searchArticle(){
    var keyword = $("input[name='keyword']").val();
    var data = {
        "keyword":keyword,
        "page":page
    };
    var searchArticle = $.ajax({
        url:"searchArticle",
        type:"get",
        data: data
    })
    searchArticle.done(function (result) {
        searchArticleList = result.extend.searchArticleList;
        var cloneArticleSearch = cloneArticle(searchArticleList,"加载更多信息");
        page++;
        $("#articleSearch").append(cloneArticleSearch);
        $("#more").attr('onclick',"moreSearchArticle()");   //更改最后一栏的 onclick事件
        dianZan();
    })
}

/**
 * 是否还有搜索的文章
 */
function moreSearchArticle(){
    var keyword = $("input[name='keyword']").val();
    var data = {
        "keyword":keyword,
        "page":page
    };
    var isMore = $.ajax({
        url:"searchArticle",
        type:"get",
        data: data
    })
    isMore.done(function (result) {
        if (result.code == 200){
            $("#more").html("没有更多的信息");
        } else {
            $("#more").remove();
            searchArticle();
        }
    })
}

function findGoodComment(obj){
    var articleId = $(obj).parents("article").find(".mycontext input").val();
    var godComment = $.ajax({
        url : "godComment",
        type:"get",
        data:{
            "articleId":articleId
        }
    });
    godComment.done(function (result) {
        if (result.code == 100){
            var godComment = result.extend.godComment;
            var s = cloneGodComment(godComment);
            $(obj).parents("article").append(s);
            $(obj).removeAttr("onclick");
        } else {
            alert("暂无神评")
        }
    })
}


